package faf.business.tests;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.EntityManager;

import junit.framework.Assert;

import org.junit.BeforeClass;
import org.junit.Test;

import faf.business.SheetManager;
import faf.dao.JpaUtils;
import faf.entities.Account;
import faf.entities.Sheet;
import faf.entities.SheetState;

public class SheetManagerTests {

	static JpaUtils jpautils;
	static SheetManager manager;

	@BeforeClass
	public static void beforeClass() {
		HashMap<String, String> config = new HashMap<String, String>();
		config.put("hibernate.hbm2ddl.auto", "create-drop");
		jpautils = new JpaUtils(config);
		manager = new SheetManager();
		manager.setJpautils(jpautils);
	}
	
	@Test
	public void testList() {
		Account account = new Account();
		
		List<Sheet> sheets = new LinkedList<Sheet>();
		
		Sheet sheet0 = new Sheet();
		sheet0.setAccount(account);
		sheets.add(sheet0);
		
		Sheet sheet1 = new Sheet();
		sheet1.setAccount(account);
		sheets.add(sheet1);
		
		account.setSheetList(sheets);
		
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(account);
		em.getTransaction().commit();
		em.close();
		
		sheets = manager.list(account);
		
		Assert.assertEquals(2, sheets.size());
		
		em = jpautils.createEntityManger();
		em.getTransaction().begin();
		account = em.merge(account);
		em.remove(account);
		em.getTransaction().commit();
		em.close();
	}
	
	@Test
	public void testListEmpty() {
		Account account = new Account();
		
		List<Sheet> sheets = new LinkedList<Sheet>();
				
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(account);
		em.getTransaction().commit();
		em.close();
		
		sheets = manager.list(account);
		
		Assert.assertEquals(0, sheets.size());
		
		em = jpautils.createEntityManger();
		em.getTransaction().begin();
		account = em.merge(account);
		em.remove(account);
		em.getTransaction().commit();
		em.close();
	}
	
	@Test
	public void testGet(){
		Sheet sheet = new Sheet();
		
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(sheet);
		em.getTransaction().commit();
		em.close();
		
		sheet = manager.get(sheet.getId());
		Assert.assertNotNull(sheet);

		em = jpautils.createEntityManger();
		em.getTransaction().begin();
		sheet = em.merge(sheet);
		em.remove(sheet);
		em.getTransaction().commit();
		em.close();		
	}
	
	@Test
	public void testGetEmpty() {
		Sheet sheet = manager.get(123);
		Assert.assertNull(sheet);
	}
	
	@Test
	public void testCreateSheet() {
		Sheet sheet = new Sheet();
		
		manager.createSheet(sheet);
		
		Assert.assertNotNull(sheet.getId());
		
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		sheet = em.merge(sheet);
		em.remove(sheet);
		em.getTransaction().commit();
	}
	
	@Test
	public void testSave() {
		Sheet sheet = new Sheet();
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(sheet);
		em.getTransaction().commit();
		
		sheet.setState(SheetState.COMPLETE);
		
		sheet = manager.save(sheet);
		
		Assert.assertEquals(SheetState.COMPLETE, sheet.getState());
		
		em = jpautils.createEntityManger();
		em.getTransaction().begin();
		sheet = em.merge(sheet);
		em.remove(sheet);
		em.getTransaction().commit();
	}
}
